package com.ygj.shop.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.base.common.persistent.model.TShops;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.datesource.MultipleDataSource;
import com.base.common.persistent.query.TShopsQuery;
import com.cjy.common.bean.OpAttributes;
import com.ygj.shop.service.CompoundsShopService;
import com.ygj.shop.service.ShopService;

@Action(value = "shopAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class ShopAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private ShopService shopService;
	
	@Resource
	private CompoundsShopService compoundsShopService;
	
	private int page;
	
	private int compoundsId;

	private int categories;	// 0 城际汇

    private String cityName;

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getCategories() {
		return categories;
	}

	public void setCategories(int categories) {
		this.categories = categories;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public String getShopsList() {
		try {
            List<TShops> results = new ArrayList<>();
            List<Integer> shopIDs = new ArrayList<>();
            if (compoundsId == 0) {
                shopIDs = compoundsShopService.getShopIDsByCityName(cityName);
                shopIDs = shopIDs.stream().distinct().collect(Collectors.toList());
            } else {
                shopIDs = compoundsShopService.getShopIDsByCompoundsId(compoundsId);
            }

            if (shopIDs.size() > 0) {
                TShopsQuery shopsQuery = new TShopsQuery();
                shopsQuery.setcIdList(shopIDs);
                shopsQuery.setcEnable(new Byte("0"));
                shopsQuery.setOffset((page-1) * Integer.valueOf(StringUtils.strip(ConfigConstants.INFOS_LIST_NUM)));
                MultipleDataSource.setDataSourceKey("shopDataSource");
                List<TShops> tShopsList = shopService.getShopsByCondition(shopsQuery);
                MultipleDataSource.setDataSourceKey("sqlserverDataSource");
                for (int i = 0; i < shopIDs.size(); i++) {
                    for (TShops tShops:
                            tShopsList) {
                        Integer cid = tShops.getcId();
                        if (cid == shopIDs.get(i).intValue()) {
                            if (compoundsId != 0) {
                                double marketValueProportion = compoundsShopService.getMarketValueProportion(compoundsId, cid);
                                tShops.setMarketValueProportion(marketValueProportion);
                            }
                            results.add(tShops);
                            break;
                        }
                    }
                }
                actionResult = ActionResult.getActionResult(new OpAttributes().add(
                        "code", 1).add("result", results));
            } else {
                actionResult = ActionResult.getActionResult(new OpAttributes().add(
                        "code", 1).add("result", new ArrayList()));
            }

			
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	
	public String getShopsListCounts() {
		try {
            List<Integer> shopIDs = new ArrayList<>();
            if (compoundsId == 0) {
                shopIDs = compoundsShopService.getShopIDsByCityName(cityName);
                shopIDs = shopIDs.stream().distinct().collect(Collectors.toList());
            } else {
                shopIDs = compoundsShopService.getShopIDsByCompoundsId(compoundsId);
            }
			if (shopIDs.size() > 0) {
				TShopsQuery shopsQuery = new TShopsQuery();
				shopsQuery.setcIdList(shopIDs);
				shopsQuery.setcEnable(new Byte("0"));

				MultipleDataSource.setDataSourceKey("shopDataSource");
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 1).add("counts", shopService.getShopsByConditionCount(shopsQuery)));
				MultipleDataSource.setDataSourceKey("sqlserverDataSource");

			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 1).add("counts", 0));
			}
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public String getSpecialShopList(){
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add("result", shopService.getSpecialShopList(categories)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
}
